import { DELETE, GET, Params, POST, Result } from '@/plugins/axios';
import type { UserApiType } from './namespace';
import type { ResponseResult } from '@/types/response';
import { handleResponseMessage } from '@/utils/common';

class userApi {
  @GET('/auth/info')
  async info(@Result res?: ResponseResult<UserApiType.Info>) {
    const permissions = (res?.data?.roles || []).map((item) => item.name);
    const result = res?.data || null;
    result && (result.permissions = permissions);
    return result;
  }

  @GET('/auth/users')
  async getUsers(@Params data: any, @Result res?: ResponseResult<UserApiType.Info[]>) {
    return {
      list: res?.data || [],
      total: 1,
    };
  }

  @GET('/auth/code')
  async getCode(@Result res?: string) {
    return res;
  }

  @GET('/auth/roles')
  async getRoles(@Result res?: ResponseResult<UserApiType.Roles[]>) {
    return res?.data ?? [];
  }

  @POST('/auth/update-role')
  async updateRoles(@Params data: any, @Result res?: ResponseResult<any>) {
    return res;
  }

  @POST('/auth/login')
  async login(@Params data: any, @Result res?: ResponseResult<UserApiType.LoginInterface>) {
    return res?.data?.token;
  }

  @POST('/auth/register')
  async register(@Params data: any, @Result res?: ResponseResult<UserApiType.LoginInterface>) {
    return res?.data.token;
  }

  async logout() {
    // TODO: logout
  }

  @POST('/auth/update')
  async update(@Params data: UserApiType.ChangeInfo, @Result res?: ResponseResult<UserApiType.LoginInterface>) {
    handleResponseMessage(res);
    return res;
  }

  @POST('/auth/update-password')
  async updatePassword(@Params data: any, @Result res?: ResponseResult<any>) {
    handleResponseMessage(res);
    return res;
  }

  @POST('/auth/update-status')
  async updateStatus(@Params data: any, @Result res?: ResponseResult<any>) {
    return res;
  }

  @POST('/auth/add-role')
  async addRole(@Params data: any, @Result res?: ResponseResult<any>) {
    return res;
  }

  @DELETE('/auth/delete-role')
  async deleteRole(@Params data: any, @Result res?: ResponseResult<any>) {
    return res;
  }
}

export default new userApi();
